import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
public class QQRegisterWindow extends JFrame implements ActionListener {
    public Socket p ;
    public DatePacket d ;
    OutputStream outputStream;
    InputStream inputStream;
    private JLabel qqLogoLabel = new JLabel(new ImageIcon("src/图片1.jpg")); // 替换为你的QQ Logo图片路径
    private JLabel usernameLabel = new JLabel("昵称:");
    private JTextField usernameField = new JTextField(20);
    private JLabel passwordLabel = new JLabel("密码:");
    private JPasswordField passwordField = new JPasswordField(20);
    private JLabel confirmPasswordLabel = new JLabel("确认密码:");
    private JPasswordField confirmPasswordField = new JPasswordField(40);
    private JButton registerButton = new JButton("注册");
    private JButton backToLoginButton = new JButton("返回登录");

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == registerButton) {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());
            // 处理注册逻辑，例如验证用户名、密码和确认密码等。
            int length = username.length();
            int length1 = password.length();
            if (password.equals(confirmPassword)) {
                if(length == 0 || length >20 || length1 < 6 || length1 >20){
                    JOptionPane.showMessageDialog(this,"用户名长度为0到20"+"\n"+"密码长度应为6到20");
                }else{
                    d.direct = 0;
                    d.name = username;
                    d.passward = password;
                    try {
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                        objectOutputStream.writeObject(d);
                        ObjectInputStream ois = new ObjectInputStream(inputStream);
                        d= (DatePacket) ois.readObject();
                    }catch (IOException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                    if(d.direct == 0) {
                        JOptionPane.showMessageDialog(this, "你的账号："+d.account +"\n"+ "注册成功,请返回登录");
                    }
                    else if(d.direct == 100){
                        JOptionPane.showMessageDialog(this,d.message);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this,"密码不匹配，请重新输入！");
            }
        } else if (e.getSource() == backToLoginButton) {
            // 返回登录界面。你可以在这里关闭当前窗口并打开登录窗口。  
            dispose(); // 关闭当前窗口。
            new LoginWindow(p,d,outputStream,inputStream); // 打开登录窗口。请确保你已经定义了QQLoginWindow类。
        }
    }
    public QQRegisterWindow(Socket p,DatePacket d,OutputStream outputStream,InputStream inputStream){
        this.p = p;
        this.d = d;
        this.outputStream=outputStream;
        this.inputStream=inputStream;
        setTitle("注册");
        setSize(400, 600); // 调整窗口大小以适应你的设计需求
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        setLocationRelativeTo(null); // 窗口居中显示
        // 设置布局为null，以便自定义组件位置
        setLayout(null);
        // 设置组件的位置和大小（这里只是示例，你可以根据自己的需求调整）
        qqLogoLabel.setBounds(120, 20, 160, 160); // 设置QQ Logo的位置和大小
        usernameLabel.setBounds(60, 200, 60, 30);
        usernameField.setBounds(120, 200, 200, 30);
        passwordLabel.setBounds(60, 240, 60, 30);
        passwordField.setBounds(120, 240, 200, 30);
        confirmPasswordLabel.setBounds(60, 280, 80, 30);
        confirmPasswordField.setBounds(120, 280, 200, 30);
        registerButton.setBounds(140, 320, 80, 30);
        backToLoginButton.setBounds(140, 360, 80, 30);
        // 将组件添加到窗口中
        add(qqLogoLabel);
        add(usernameLabel);
        add(usernameField);
        add(passwordLabel);
        add(passwordField);
        add(confirmPasswordLabel);
        add(confirmPasswordField);
        add(registerButton);
        add(backToLoginButton);
        // 添加事件监听器
        registerButton.addActionListener(this);
        backToLoginButton.addActionListener(this);
    }

}